package org.finalministry;

import geodb.GeoDB;

import java.io.File;

import org.finalministry.database.FinalministryDatabase;
import org.finalministry.database.connection.FinalministryDatabaseConnection;
import org.finalministry.database.dao.DaoFactory;
import org.finalministry.gui.GuiMain;
import org.finalministry.utils.DatabaseInitializer;
import org.finalministry.web.StartWebServerGui;

public class Main {

	public static Main main = null;
	public String localPath = null;

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		System.out.println("Finalministry Desktop Version 0.1 Copyright Pietro Lusso 2014");

		// FIXME: get User Password from a Login-Dialog

		// FinalministryDatabase database = new
		// FinalministryDatabase("finalministryProd", "finalministry",
		// "finalministry", 7786);
		// database.start();
		// DaoFactory.setFinalMinistryConnection(new
		// FinalministryDatabaseConnection("finalministryProd", "finalministry",
		// "finalministry", 7786));

		FinalministryDatabase database = new FinalministryDatabase("finalministryTest", "sa", "sa", 7786);
		database.start();
		DaoFactory.setFinalMinistryConnection(new FinalministryDatabaseConnection("finalministryTest", "sa", "sa", 7786));
		DaoFactory.getInstance();
		System.out.println(DaoFactory.getInstance().isConnectionActive());
		// DatabaseInitializer.forceDatabaseInitialization(DaoFactory.getFinalMinistryConnection());
		DatabaseInitializer.forceDatabaseSpatialOnlyInitialization(DaoFactory.getFinalMinistryConnection());

		/*
		 * DAS IST DER FIX !!!
		 */
		try {
			GeoDB.InitGeoDB(DaoFactory.getFinalMinistryConnection().getConnection());
		} catch (Exception e) {
			System.err.println("Error while initializing spatial methods for database ... ");
			e.printStackTrace();
			database.stopServer();
			System.exit(0);
		}

		// MinimalServerRest.initRestServerHttp();
		GuiMain gui = new GuiMain(database);
		StartWebServerGui guiWeb = new StartWebServerGui(database);

	}

	public void setLocalPath(String path) {
		localPath = path;
	}

	public static String getLocalPath() {

		if (main == null) {
			File local = new File("");
			main = new Main();
			main.setLocalPath(local.getAbsolutePath());
		}

		return main.localPath + "/";
	}
}
